package com.iflytek.uoamp.agent.common.util;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class EncryptHelper {

	private EncryptHelper() {

	}

	public static byte[] desEncrypt(byte[] expresslyBytes, byte[] key) {
		try {
			SecureRandom random = new SecureRandom();
			DESKeySpec desKey = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey securekey = keyFactory.generateSecret(desKey);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
			return cipher.doFinal(expresslyBytes);
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return new byte[0];
		}
	}

	public static byte[] desDecrypt(byte[] ciphertextBytes, byte[] key) {
		try {
			SecureRandom random = new SecureRandom();
			DESKeySpec desKey = new DESKeySpec(key);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey securekey = keyFactory.generateSecret(desKey);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, securekey, random);
			return cipher.doFinal(ciphertextBytes);
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return new byte[0];
		}
	}

	public static String encryptMD5(byte[] expresslyBytes) {
		char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(expresslyBytes);

			byte[] md = mdTemp.digest();
			int j = md.length;
			char[] str = new char[j * 2];
			int k = 0;

			for (int i = 0; i < j; i++) {
				byte b = md[i];
				str[k++] = hexDigits[b & 0xf];
			}

			return new String(str);

		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return new String();
		}
	}

	public static Key getKey(byte[] b) {
		byte[] temp = new byte[8];
		for (int i = 0; i < temp.length && i < b.length; i++) {
			temp[i] = b[i];
		}
		return new SecretKeySpec(temp, "DES");
	}

	public static String byte2hex(byte[] b) {
		String temp;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; i++) {
			temp = Integer.toString(b[i] & 0xff, 16);
			if (temp.length() == 1) {
				temp = "0" + temp;
			}
			sb.append(temp);
		}
		return sb.toString();
	}

	public static String desEncrptNotIV(String key, String s) {
		try {
			Key key2 = getKey(key.getBytes());
			Cipher cen = Cipher.getInstance("DES");

			cen.init(Cipher.ENCRYPT_MODE, key2);
			byte[] enb = cen.doFinal(s.getBytes());

			return byte2hex(enb);
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return new String();
		}
	}

	public static String getBASE64(String src) {
		if (src == null) {
			return new String();
		}

		try {
			return (new BASE64Encoder().encode(src.getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return new String();
		}
	}

	public static String getFromBASE64(String src) {
		if (src == null)
			return new String();
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(src);
			return new String(b);
		} catch (Exception e) {
			Logger.getRootLogger().error(e.getMessage(),e);
			return null;
		}
	}
}
